package blog.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import blog.entity.Menu;
import blog.service.MenuService;

@RequestMapping("menu")
@Controller
public class MenuController {
	@Autowired
	private MenuService menuService;
	/**
	 * 查看所有的菜单
	 * @param map
	 * @return
	 */
	@RequestMapping("")
	public String menuList(ModelMap map) {
		List<Menu> findAllMenu = menuService.findAllMenu();
		map.put("menuList",findAllMenu);
		return "menu/menu_list";
	}
	/**
	 * 添加菜单
	 * @param menu
	 * @return
	 */
	@RequestMapping("/insertSubmit")
	public String addMenu(Menu menu) {
		menuService.addMenu(menu);
		return "forward:/menu";
	}
    /**
     * 删除菜单
     */
    @RequestMapping(value = "/delete/{id}")
	public String deleteMenu(@PathVariable("id") Integer id)  {
	    menuService.deleteMenu(id);
	    return "forward:/menu";
	}

    /**
     * 转到菜单修改页
     */
    @RequestMapping(value = "/edit/{id}",method=RequestMethod.GET)
	public String toMenuEditPage(@PathVariable("id") Integer id,ModelMap m)  {
	    Menu menu =  menuService.getMenuById(id);
	    List<Menu> menuList = menuService.findAllMenu();
	    
	    m.put("menuList", menuList);
	    m.put("menu", menu);
	    
	    return "menu/menu_edit";
	}

    /**
     * 真正的进行菜单更新
     */
	 @RequestMapping(value = "/edit",method = RequestMethod.POST)
	 public String editMenu(Menu menu)  {
	    menuService.updateMenu(menu);
	    return "forward:/menu";
	 }
	
}
